package com.lqjava.flowable.beans.utils;
import org.apache.commons.codec.binary.Base64;

import javax.crypto.Cipher;
import javax.crypto.Mac;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.security.Security;
//import java.util.Base64;

/**
 * AES加解密工具
 */
public class EncryptUtil {

    private static final String CipherMode="AES/CBC/PKCS7Padding";
    private static final String EncryptAlg ="AES";
    private static final String Encode="UTF-8";

    /*appkey在aes加密中不需要使用*/
    private  static final String APPKEY="ecf2ebc1b26b46df9cba1f19b905c049";
    private static final String APPSECRET = "f9e780a7b32942fba7760d895dfc9b5a";
    private static final String AESIV = "ff465fdecc764337";



    /**
     * 加密：有向量16位，结果转base64
     * @param context
     * @return
     */
    public static String encrypt(String context) {
        try { //下面这行在进行PKCS7Padding加密时必须加上，否则报错
            Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
            byte[] content=context.getBytes(Encode);
            Cipher cipher = Cipher.getInstance(CipherMode);
            cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(APPSECRET.getBytes(Encode), EncryptAlg), new IvParameterSpec(AESIV.getBytes(Encode)));
            byte[] data = cipher.doFinal(content);

            //Base64.Encoder encoder = Base64.getEncoder();
            //String result = encoder.encodeToString(data);
            String result= Base64.encodeBase64String(data);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    public static byte[] decodeBase64(final String base64String) {
        return new Base64().decode(base64String);
    }
    /**
     * 解密
     * @param context
     * @return
     */
    public static String decrypt(String context) {
        try {
            Cipher cipher = Cipher.getInstance(CipherMode);
            cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(APPSECRET.getBytes(Encode), EncryptAlg), new IvParameterSpec(AESIV.getBytes(Encode)));
            byte[] data1=Base64.decodeBase64(context);
            byte[] content = cipher.doFinal(data1);
            String result=new String(content,Encode);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    //获取当前时间戳
    public static long getTimestamp(){
        return System.currentTimeMillis() / 1000;
    }

    //HmacSha256
    public static String sha256_HMAC(String data, String key) throws Exception {
        Mac sha256_HMAC = Mac.getInstance("HmacSHA256");
        SecretKeySpec secret_key = new SecretKeySpec(key.getBytes("UTF-8"), "HmacSHA256");
        sha256_HMAC.init(secret_key);
        byte[] array = sha256_HMAC.doFinal(data.getBytes("UTF-8"));
        StringBuilder sb = new StringBuilder();
        for (byte item : array) {
            sb.append(Integer.toHexString((item & 0xFF) | 0x100).substring(1, 3));
        }
        return sb.toString();
    }

    public static void main(String[] args) {
        final String name = "刘庆";
        final String idNumber = "513433199212202716";
        final String phoneNumber = "17761204652";
        final String merchantId = APPKEY;
        String fmt = "{\"name\":\"%s\",\"idNumber\":\"%s\",\"phoneNumber\":\"%s\",\"merchantId\":\"%s\"}";
        String s = String.format(fmt,name,idNumber,phoneNumber,merchantId);
        System.out.println("appkey："+APPKEY);
        System.out.println("appsecret："+APPSECRET);
        System.out.println("-----------------------------------");
        String ss=(encrypt(s));
        System.out.println("Aes加密后");
        System.out.println(ss);
        System.out.println("-----------------------------------");
        System.out.println("Aes解密后");
        System.out.println(decrypt(ss));
        Long timestamp=getTimestamp();
        System.out.println("-----------------------------------");
        System.out.println("timestamp");
        System.out.println(timestamp);
        String data=APPKEY+timestamp+APPSECRET;
        String hmacsha256s;
        try
        {
            hmacsha256s=(sha256_HMAC("394d8267476f4556afbd3a3820126d72"+"1625561853"+"c62a225426ca432e952c441f0164e03d","394d8267476f4556afbd3a3820126d72"));
            System.out.println("-----------------------------------");
            System.out.println("HmacSha256");
            System.out.println(hmacsha256s);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }

        System.out.println(String.format("https://toolh5-ets.wetax.com.cn/?data=%s&appkey=%s",ss,Base64.encodeBase64String(APPKEY.getBytes(StandardCharsets.UTF_8))));





    }
}

